package com.yfbao.horizon.compress;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CompressTest {

    public static void main(String[] args) {
        compare();
//        testSerialization();
    }


    private static void testSerialization(){
//                testKryserialization();
//        testHessianserialization();
//        testFSTserialization();
//        testMsgPackserialization();
        serializaitonPerf();
    }

    private static void serializaitonPerf(){
        int runTime=3000;
        Map<Integer,Integer> map = new HashMap<>();
        map.put(1000,runTime);
        map.put(2000,runTime);
        map.put(3000,runTime);
        map.put(5000,runTime);
        map.put(7000,runTime);
        map.put(9000,runTime);
        map.put(10000,runTime);

        StringBuilder builder=new StringBuilder();
        builder.append("KryoSerialization serizliation test:\r\n");
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            Integer key = entry.getKey();
            Integer value = entry.getValue();
            List<CtpEntityResource> testData = LZ4Utils.createTestData(key);
            long start = System.currentTimeMillis();
            for(int i=0;i<value;i++){
                byte[] bytes = KryoSerialization.serializeObject(testData);
                List<CtpEntityResource> list = (List<CtpEntityResource>)KryoSerialization.deserializeObject(bytes);
            }
            long cost = System.currentTimeMillis() - start;
            System.out.println(key+",cost:"+cost);
            builder.append("srcSize:"+key).append(" ").append(",runTimes:"+value).append(" ")
                    .append(",cost:"+cost).append(" ;\r\n");
        }
        builder.append("\r\n");
        builder.append("HessianSerialization serizliation test:\r\n");
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            Integer key = entry.getKey();
            Integer value = entry.getValue();
            List<CtpEntityResource> testData = LZ4Utils.createTestData(key);
            long start = System.currentTimeMillis();
            for(int i=0;i<value;i++){
                byte[] bytes = HessianSerialization.serializeObject(testData);
                List<CtpEntityResource> list = (List<CtpEntityResource>)HessianSerialization.deserializeObject(bytes);
            }
            long cost = System.currentTimeMillis() - start;
            System.out.println(key+",cost:"+cost);
            builder.append("srcSize:"+key).append(" ").append(",runTimes:"+value).append(" ")
                    .append(",cost:"+cost).append(" ;\r\n");
        }

        builder.append("\r\n");
        builder.append("FSTSerizlization serizliation test:\r\n");
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            Integer key = entry.getKey();
            Integer value = entry.getValue();
            List<CtpEntityResource> testData = LZ4Utils.createTestData(key);
            long start = System.currentTimeMillis();
            for(int i=0;i<value;i++){
                byte[] bytes = FSTSerizlization.serialize(testData);
                List<CtpEntityResource> list = (List<CtpEntityResource>)FSTSerizlization.deserialize(bytes);
            }
            long cost = System.currentTimeMillis() - start;
            System.out.println(key+",cost:"+cost);
            builder.append("srcSize:"+key).append(" ").append(",runTimes:"+value).append(" ")
                    .append(",cost:"+cost).append(" ;\r\n");
        }

        System.out.println(builder.toString());

    }


    private static void compare(){
        int runTime=3000;
        Map<Integer,Integer> map = new HashMap<>();
        map.put(500,runTime);
        map.put(1000,runTime);
        map.put(2000,runTime);
        map.put(3000,runTime);
        map.put(5000,runTime);
        map.put(7000,runTime);
        map.put(9000,runTime);
        map.put(10000,runTime);
        System.out.println("begin do LZ4 test");
        StringBuilder builder=new StringBuilder();
        builder.append("LZ4 TEST:\r\n");
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            Integer key = entry.getKey();
            Integer value = entry.getValue();
            List<CtpEntityResource> testData = LZ4Utils.createTestData(key);
            String compressRatio = LZ4Utils.getCompressRatio(testData);
            long cost = LZ4Utils.compressData(testData, value);
            builder.append("srcSize:"+key).append(" ").append("runTimes:"+value).append(" ")
             .append(",ratio:"+compressRatio).append(" ").append(",cost:"+cost).append(" ;\r\n");
        }
        builder.append("\r\n");
        builder.append("\r\n");

        System.out.println("begin do Snappy test");
        builder.append("Snappy TEST:\r\n");
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            Integer key = entry.getKey();
            Integer value = entry.getValue();
            List<CtpEntityResource> testData = SnappyUtils.createTestData(key);
            String compressRatio = SnappyUtils.getCompressRatio(testData);
            long cost = SnappyUtils.compressData(testData, value);
            builder.append("srcSize:"+key).append(" ").append("runTimes:"+value).append(" ")
                    .append(",ratio:"+compressRatio).append(" ").append(",cost:"+cost).append(" ;\r\n");
        }

        System.out.println(builder.toString());
    }


    public static void testKryserialization(){
        List<CtpEntityResource> testData = LZ4Utils.createTestData(500);
        byte[] bytes = KryoSerialization.serializeObject(testData);
        System.out.println("after serialization size:"+bytes.length);
        List<CtpEntityResource> list = (List<CtpEntityResource>)KryoSerialization.deserializeObject(bytes);
        System.out.println(list.get(0).getEntityId());
    }

    public static void testHessianserialization(){
        List<CtpEntityResource> testData = LZ4Utils.createTestData(500);
        byte[] bytes = HessianSerialization.serializeObject(testData);
        System.out.println("after serialization size:"+bytes.length);
        List<CtpEntityResource> list = (List<CtpEntityResource>)HessianSerialization.deserializeObject(bytes);
        System.out.println(list.get(0).getEntityId());
    }

    public static void testFSTserialization(){
        List<CtpEntityResource> testData = LZ4Utils.createTestData(500);
        byte[] bytes = FSTSerizlization.serialize(testData);
        System.out.println("after serialization size:"+bytes.length);
        List<CtpEntityResource> list = (List<CtpEntityResource>)FSTSerizlization.deserialize(bytes);
        System.out.println(list.get(0).getEntityId());
    }

    public static void testMsgPackserialization(){
        List<CtpEntityResource> testData = LZ4Utils.createTestData(500);
//        byte[] bytes = MessagePackSerizlization.serialize(testData);
//        System.out.println("after serialization size:"+bytes.length);
//        List deserialize = MessagePackSerizlization.deserialize(bytes, List.class);
//        System.out.println(deserialize.get(0));
//        List<CtpEntityResource> list = (List<CtpEntityResource>)MessagePackSerizlization.deserialize(bytes,List.class);
//        System.out.println(list.get(0).getEntityId());

        byte[] bytes = MessagePackSerizlization.serialize(testData.get(0));
        System.out.println("after serialization size:"+bytes.length);
        CtpEntityResource deserialize = MessagePackSerizlization.deserialize(bytes, CtpEntityResource.class);
        System.out.println(deserialize.getEntityId());
    }
}
